//
//  LDProjectView.m
//  Gift
//
//  Created by 李迪 on 15/11/12.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "LDProjectView.h"
#import "BaseHeader.h"

@interface LDProjectView ()

@property (nonatomic, retain)UIButton *check;
@property (nonatomic, retain)UIButton *image;
@property (nonatomic, retain)UIScrollView *LBScrollView;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)NSMutableArray *CheckAll;
@property (nonatomic, retain)UILabel *tittle;

@end

@implementation LDProjectView

- (void)dealloc{
    
    [_check release];
    [_image release];
    [_LBScrollView release];
    [_array release];
    [_tittle release];
    [super dealloc];
}



- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self craeteProject];
    }
    
    return self;
}

- (void)craeteProject
    {
        [PFAFNetworking getUrl:@"http://api.liwushuo.com/v2/collections?limit=6&offset=0" body:nil response:PFJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
            self.array = [[NSMutableArray array]init];
            for (NSDictionary *dic in [[resposeObject objectForKey:@"data"]objectForKey:@"collections"]) {
                ClassificationModel *model = [ClassificationModel modelSetDic:dic];
                
                [self.array addObject:model];
            }
            [self LBView];
            [self.LBScrollView setContentSize:CGSizeMake(20 + self.array.count * SCREEN_WIDTH / 2, 0)];

            for (int i = 0; i < self.array.count ; i++) {
                self.image = [UIButton buttonWithType:UIButtonTypeCustom];
                
                self.image.frame = CGRectMake(10 + (i * (SCREEN_WIDTH / 2)), 0, SCREEN_WIDTH / 2, 80);
                ClassificationModel *model = [self.array objectAtIndex:i];
                [self.image sd_setImageWithURL:[NSURL URLWithString:(model.banner_image_url)]  forState:UIControlStateNormal];
                self.image.layer.cornerRadius = 8;
                self.image.layer.masksToBounds = YES;
                self.image.tag = 200 + i;
                [self.LBScrollView addSubview:self.image];
                [self.image addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
            }
            self.tittle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 20)];
            self.tittle.text = @"专题";
            [self addSubview:self.tittle];
            
            self.check = [UIButton buttonWithType:UIButtonTypeCustom];
            self.check.frame = CGRectMake(SCREEN_WIDTH - 80, 10, 80, 20);
            [self.check setTitle:@"查看全部>" forState:UIControlStateNormal];
            self.check.titleLabel.font = [UIFont systemFontOfSize:13];
            [self.check setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
            [self addSubview:self.check];
           [self.check addTarget:self action:@selector(handleCheck) forControlEvents:UIControlEventTouchUpInside];
            
            [_tittle release];
            [_LBScrollView release];
            
            //夜间
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString *passWord = [user objectForKey:@"night"];
            if ([passWord isEqualToString:@"yes"]) {
                [self.check setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
                self.tittle.textColor = [UIColor whiteColor];
                
            }
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNot:) name:@"lidi" object:nil];
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
    }

//夜间方法
- (void)handleNot:(NSNotification *)Not{
    NSString *passWord = [Not object];
    if ([passWord isEqualToString:@"yes"]) {
        [self.check setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
        self.tittle.textColor = [UIColor whiteColor];
    }if ([passWord isEqualToString:@"no"]) {
        [self.check setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
        self.tittle.textColor = [UIColor blackColor];
    }
    
}



#pragma mark - 点击butt后执行的代理方法

- (void)handleCheck{
    
    [self.ButtonCheckdelegate hadleButtonCheck];
   
}



- (void)handleButton:(UIButton *)button{

    ClassificationModel *model = [self.array objectAtIndex:(button.tag - 200)];

    [self.imageDelegate handleImagePushNum:model.Cid];

}







- (void)LBView
{
    _LBScrollView = [[UIScrollView alloc] init];
    [_LBScrollView setFrame:CGRectMake(0, 40, SCREEN_WIDTH, 120)];
    [_LBScrollView setPagingEnabled:YES];
    [self addSubview:_LBScrollView];
}










/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
